home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / searchengines.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  5.8 KB  |  170 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. import views
  19. import indexes
  20. import re
  21. import template
  22. from util import getSingletonDDBObject, checkU, returnsUnicode
  23. from database import DDBObject
  24. from xhtmltools import urlencode
  25. from templatehelper import quoteattr, escape
  26. from xml.dom.minidom import parse
  27. import resources
  28. import os
  29. import config
  30. import prefs
  31. import logging
  32.  
  33. class SearchEngine(DDBObject):
  34.     def __init__(self, name, title, url, sortOrder=0):
  35.         checkU(name)
  36.         checkU(title)
  37.         checkU(url)
  38.         self.name = name
  39.         self.title = title
  40.         self.url = url
  41.         self.sortOrder = sortOrder
  42.         DDBObject.__init__(self)
  43.  
  44.     def getRequestURL (self, query, filterAdultContents, limit):
  45.         requestURL = self.url.replace(u"%s", urlencode(query))
  46.         requestURL = requestURL.replace(u"%a", unicode(int(not filterAdultContents)))
  47.         requestURL = requestURL.replace(u"%l", unicode(int(limit)))
  48.         return requestURL
  49.  
  50. def deleteEngines():
  51.     for engine in views.searchEngines:
  52.         engine.remove()
  53.  
  54. def searchForSearchEngines (dir):
  55.     engines = {}
  56.     try:
  57.         for f in os.listdir (dir):
  58.             if f.endswith(".xml"):
  59.                 engines[os.path.normcase(f)] = os.path.normcase(os.path.join(dir, f))
  60.     except OSError:
  61.         pass
  62.     return engines
  63.  
  64. def warn (file, message):
  65.     logging.warn ("Error parsing searchengine: %s: %s", file, message)
  66.  
  67. def loadSearchEngine (file):
  68.     try:
  69.         dom = parse(file)
  70.         id = displayname = url = sort = None
  71.         root = dom.documentElement
  72.         for child in root.childNodes:
  73.             if child.nodeType == child.ELEMENT_NODE:
  74.                 tag = child.tagName
  75.                 text = child.childNodes[0].data
  76.                 if tag == "id":
  77.                     if id != None:
  78.                         warn(file, "Duplicated id tag")
  79.                         return
  80.                     id = text
  81.                 elif tag == "displayname":
  82.                     if displayname != None:
  83.                         warn(file, "Duplicated displayname tag")
  84.                         return
  85.                     displayname = text
  86.                 elif tag == "url":
  87.                     if url != None:
  88.                         warn(file, "Duplicated url tag")
  89.                         return
  90.                     url = text
  91.                 elif tag == "sort":
  92.                     if sort != None:
  93.                         warn(file, "Duplicated sort tag")
  94.                         return
  95.                     sort = float (text)
  96.                 else:
  97.                     warn(file, "Unrecognized tag %s" % (tag,))
  98.                     return
  99.         dom.unlink()
  100.         if id == None:
  101.             warn(file, "Missing id tag")
  102.             return
  103.         if displayname == None:
  104.             warn(file, "Missing displayname tag")
  105.             return
  106.         if url == None:
  107.             warn(file, "Missing url tag")
  108.             return
  109.         if sort == None:
  110.             sort = 0
  111.         SearchEngine (id, displayname, url, sort)
  112.     except:
  113.         warn(file, "Exception parsing file")
  114.  
  115. def createEngines():
  116.     deleteEngines()
  117.     searchEngines = searchForSearchEngines(resources.path("searchengines"))
  118.     searchEngines.update (searchForSearchEngines(os.path.join (config.get(prefs.SUPPORT_DIRECTORY), "searchengines")))
  119.     for file in searchEngines.itervalues():
  120.         loadSearchEngine (file)
  121.  
  122. @returnsUnicode
  123. def getRequestURL(engineName, query, filterAdultContents=True, limit=50):
  124.     if query == "LET'S TEST DTV'S CRASH REPORTER TODAY":
  125.         someVariable = intentionallyUndefinedVariableToTestCrashReporter
  126.     if query == "LET'S DEBUG DTV: DUMP DATABASE":
  127.         import database
  128.         database.defaultDatabase.liveStorage.dumpDatabase (database.defaultDatabase)
  129.         return u""
  130.     for engine in views.searchEngines:
  131.         if engine.name == engineName:
  132.             return engine.getRequestURL(query, filterAdultContents, limit)
  133.     return u""
  134.  
  135. @returnsUnicode
  136. def getSearchEnginesHTML ():
  137.     searchFeed = getSingletonDDBObject (views.feeds.filterWithIndex(indexes.feedsByURL, 'dtv:search'))
  138.     enginesHTML = u'<select name="engines" onChange="updateLastSearchEngine()">\n'
  139.     for engine in views.searchEngines:
  140.         enginesHTML += u'<option value="%s"' % (quoteattr(engine.name),)
  141.         if engine.name == searchFeed.lastEngine:
  142.             enginesHTML += u' selected="selected"'
  143.         enginesHTML += u'>'
  144.         enginesHTML += escape(engine.title)
  145.         enginesHTML += u'</option>'
  146.     enginesHTML += u'</select>'
  147.     return enginesHTML
  148.  
  149. def getLastEngineTitle():
  150.     last = getLastEngine()
  151.     for engine in views.searchEngines:
  152.         if engine.name == last:
  153.             return engine.title
  154.     return u''
  155.  
  156. def getLastEngine():
  157.     searchFeed = _getSearchFeed()
  158.     if not hasattr(searchFeed, 'lastEngine'):
  159.         return u'youtube'
  160.     return searchFeed.lastEngine
  161.  
  162. def getLastQuery():
  163.     searchFeed = _getSearchFeed()
  164.     if not hasattr(searchFeed, 'lastQuery'):
  165.         return ''
  166.     return searchFeed.lastQuery
  167.  
  168. def _getSearchFeed():
  169.     return getSingletonDDBObject (views.feeds.filterWithIndex(indexes.feedsByURL, 'dtv:search'))
  170.